package com.hbr.talk;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener, OnTouchListener {
    private Button btnBegin;
    private Button btnEnd;
    private Button btnPlay;
    private Button btnTest;
    private MediaRecorder mediarecord;
    private String fd;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnBegin = (Button) findViewById(R.id.btnBegin);
        btnEnd = (Button) findViewById(R.id.btnEnd);
        btnPlay = (Button) findViewById(R.id.btnPlay);
        btnTest = (Button) findViewById(R.id.btnTest);
        btnBegin.setOnClickListener(this);
        btnEnd.setOnClickListener(this);
        btnPlay.setOnClickListener(this);
        btnTest.setOnTouchListener(this);
        LocationManager loctionManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location location = loctionManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        btnTest.setText(location.getLatitude() + "" + location.getLongitude());
        final LocationListener locationListener = new LocationListener() {
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }

            @Override
            public void onProviderEnabled(String provider) {
            }

            @Override
            public void onProviderDisabled(String provider) {
            }

            // 当位置变化时触发
            @Override
            public void onLocationChanged(Location location) {
                // 使用新的location更新TextView显示
                updateWithNewLocation(location);
            }

            private void updateWithNewLocation(Location location) {
                Geocoder g = new Geocoder(MainActivity.this, Locale.CHINESE);
                try {
                    List lst = g.getFromLocation(location.getLatitude(), location.getLongitude(), 5);
                    btnTest.setText(lst.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        };
        loctionManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

        // Criteria criteria = new Criteria();
        // criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度
        // criteria.setAltitudeRequired(false);//不要求海拔
        // criteria.setBearingRequired(false);//不要求方位
        // criteria.setCostAllowed(true);//允许有花费
        // criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗
        // String provider = loctionManager.getBestProvider(criteria, true);
        // Location location = loctionManager.getLastKnownLocation(provider);

    }

    @Override
    public void onClick(View v) {
        try {
            if (v == btnBegin) {
                mediarecord = new MediaRecorder();
                mediarecord.setAudioSource(MediaRecorder.AudioSource.MIC);
                mediarecord.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                mediarecord.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                File sampleDir = Environment.getExternalStorageDirectory();
                fd = File.createTempFile("tmp", ".3gp", sampleDir).getAbsolutePath();
                mediarecord.setOutputFile(fd);
                mediarecord.prepare();
                btnBegin.setText("录制中…………");
                mediarecord.start();
            } else if (v == btnEnd) {
                mediarecord.stop();
                mediarecord.reset();
                mediarecord.release();
                btnBegin.setText(R.string.begin);
            } else if (v == btnPlay) {
                MediaPlayer mp = new MediaPlayer();
                File file = new File(fd);
                FileInputStream fis = new FileInputStream(file);
                mp.setDataSource(fis.getFD());
                // mp.setDataSource("http://219.138.125.22/myweb/mp3/CMP3/JH19.MP3");
                mp.prepare();
                mp.start();
            }
        } catch (IllegalArgumentException e) {
        } catch (IllegalStateException e) {
        } catch (IOException e) {
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            btnTest.setText(MotionEvent.ACTION_DOWN + "down");
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            btnTest.setText(MotionEvent.ACTION_UP + "up");
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            btnTest.setText(MotionEvent.ACTION_MOVE + "move");
        } else {
            btnTest.setText(event.getAction() + "other");
        }
        return false;
    }
}